/* $Id:status.cc $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/

#define DEBUG_ROUTINE "status"

#include <sstream>
#include <fstream>
#include "../general/util.h"
#include "../general/log.h"
#include "../general/channel.h"
#include "status.h"
#include "army.h"
#include "../general/general.h"

status::status() {
    army=0;
    level=0;
}

// Field : army

class army& status::getArmy() const { if (army==0) throw nullpointer(); return *army; }
void status::setArmy(class army &v) { army=&v; }

// Field : level

const int status::getLevel() const { return level; }
void status::setLevel(const int v) { level=v; }

void status::save(class channel &ch) {
    ch.write("status", "");
    ch.write("a", getArmy().getName());
    ch.write("l", getLevel());
}

void status::load(class channel &ch) {
    if (ch.key=="a" || ch.key=="army") {
        if (!army_repo.isArmy(ch.value)) {
            dbg_state("link unknown army %s", ch.value.c_str());
            class army rec;
            rec.setName(ch.value);
            army_repo.setArmy(rec);
        }
        setArmy(army_repo.getArmy(ch.value));
        ch.next();
    }
    if (ch.key=="l" || ch.key=="level") { setLevel(atoi(ch.value.c_str())); ch.next(); }
}

void status::xml(ofstream &f, int l, int r) {
    f << ncpy(l, "  ") << "<status level=\"" << getLevel() <<
    "\">" << endl;
    if (r<2) try {
        getArmy().xml(f,l+1,r+1);
    } catch (nullpointer n) {}
    f << ncpy(l, "  ") << "</status>" << endl;
}
